//
//  CCAutoRollingView.m
//  HimalayaMountain
//
//  Created by dllo on 16/7/13.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "CCAutoRollingView.h"
#import "CCImageViewCollectionViewCell.h"


@interface CCAutoRollingView ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property(nonatomic, retain)UICollectionViewFlowLayout *flowLayout;
@property(nonatomic, retain)UICollectionView *collectionView;

@end



@implementation CCAutoRollingView

- (void)dealloc
{
    [_urlsArray release];
    [super dealloc];
}

-(instancetype)initWithFrame:(CGRect)frame itemSize:(CGSize)itemSize collectionViewFrame:(CGRect)collectionViewFrame timeInterval:(CGFloat)timeTnterVal{
    if (self = [super initWithFrame:frame]) {
      
        self.urlsArray = [NSArray array];
        
        self.flowLayout = [[UICollectionViewFlowLayout alloc]init];
        self.flowLayout.itemSize = itemSize;
        
        
        [self setFlowLayoutProperties];
        
        
        
        self.collectionView = [[UICollectionView alloc]initWithFrame:collectionViewFrame collectionViewLayout:_flowLayout];
        
        [self setCollectionViewProperties];
        
        [self addSubview:_collectionView];
        
        
        self.collectionView.delegate = self;
        self.collectionView.dataSource = self;
        
        [self.collectionView registerClass:[CCImageViewCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
        
        
        
        
        [NSTimer scheduledTimerWithTimeInterval:timeTnterVal target:self selector:@selector(addScroll) userInfo:nil repeats:YES];
        
        [_flowLayout release];
        [_collectionView release];
        
        
    }
    return self;
}

-(void)setFlowLayoutProperties{
    
    self.flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.flowLayout.minimumInteritemSpacing = 0;
    self.flowLayout.minimumLineSpacing = 0;
    
    
}


-(void)setCollectionViewProperties{
    
    self.collectionView.showsHorizontalScrollIndicator = NO;

}
-(void)addScroll{
    self.collectionView.contentOffset = CGPointMake(self.collectionView.contentOffset.x + self.collectionView.frame.size.width, 0);
    
    
    if (_collectionView.contentOffset.x <= 1 * self.frame.size.width) {
        _collectionView.contentOffset = CGPointMake(self.urlsArray.count * self.flowLayout.itemSize.width, 0);
    }else if (_collectionView.contentOffset.x >= (self.urlsArray.count )*self.frame.size.width){
        _collectionView.contentOffset = CGPointMake(self.frame.size.width, 0);
    }

    
}


-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.urlsArray.count + 2;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    if (indexPath.row == 0) {
        CCImageViewCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];

       
        [cell.imageView setImageWithURL:[NSURL URLWithString:self.urlsArray.lastObject]];
        
        return cell;
       
    }else if(indexPath.row == self.urlsArray.count + 1){
        CCImageViewCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
        
        [cell.imageView setImageWithURL:[NSURL URLWithString:self.urlsArray.firstObject]];

        return cell;
    }else{
        
        CCImageViewCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
        
        [cell.imageView setImageWithURL:[NSURL URLWithString:[self.urlsArray objectAtIndex:indexPath.row - 1]]];
        
        
        return cell;
        
 
    }
}


-(void)setUrlsArray:(NSArray *)urlsArray
{
    if (_urlsArray != urlsArray) {
        [_urlsArray release];
        _urlsArray = [urlsArray retain];
    }
    
    [self.collectionView reloadData];
}


@end
